package jun.empdepmanagersystem02.service.impl;

import jun.empdepmanagersystem02.aop.LogFuntion;
import jun.empdepmanagersystem02.mapper.DepMapper;
import jun.empdepmanagersystem02.pojo.Dep;
import jun.empdepmanagersystem02.pojo.Result;
import jun.empdepmanagersystem02.service.DepService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service

public class DepServiceImpl implements DepService {
    @Autowired
    private DepMapper depMapper;

    @Override

    public List<Dep> selectAll() {
        return depMapper.selectAll();
    }

    @Override
    @LogFuntion
    public Result update(Dep dep) {
        if (get(dep.getId()) == null) {
            return Result.error("用户不存在");
        }
        dep.setUpdateTime(LocalDateTime.now());
        depMapper.update(dep);
        return Result.success("修改成功，新部门信息如下", dep);
    }


    @Override
    @LogFuntion
    public void delete(Integer id) {
        depMapper.delete(id);
    }

    @Override
    @LogFuntion
    //post 一般都是用json比较好
    public Result insert(Dep dep) {
        if (dep == null || dep.getName().equals("")) {
            return Result.error("失败，未填写部门名称");
        }
        dep.setCreateTime(LocalDateTime.now());
        dep.setUpdateTime(LocalDateTime.now());
        depMapper.insert(dep);
        //较为重要 返回主键
        return Result.success("插入成功，新部门信息如下", dep);

    }

    @Override
    public Dep get(Integer id) {
        return depMapper.getByID(id);
    }

    @Override
    public Dep getById(Integer id) {
        return  depMapper.getByID(id);
    }
}
